<?php
include_once('session.php');
require_once('php/class/clientes.class.php');
$oCliente = new cliente();
$los_clientes = $oCliente->buscar();

if ($los_clientes == 0) {
  $tabla = "No hay nada que ver...";
} else {
  $tabla = "<table cellpadding='0' cellspacing='0' border='0' id='table' class='reporte'>";
  $tabla .= "<thead><tr><th>Id</th>";
  $tabla .= "<th>Nombre, Apellido</th>";
  $tabla .= "<th>Mail</th>";
  $tabla .= "<th>Tel&eacute;fono</th>";
  $tabla .= "<th>Empresa</th>";
  $tabla .= "<th class='nosort'>Opciones</th></tr></thead><tbody>";
  $co = 0;
  for ($i = 0; $i < count($los_clientes['ID']); $i++) {
    if ($co) {
      $color = "par";
      $co = 0;
    } else {
      $color = "impar";
      $co = 1;
    }
    $tabla .= "<tr class='" . $color . "'><td>" . $los_clientes['ID'][$i] . "</td>";
    $tabla .= "<td>" . $los_clientes['NOMBRE'][$i] . ",&nbsp;" . $los_clientes['APELLIDO'][$i] . "</td>";
    $tabla .= "<td>" . $los_clientes['MAIL'][$i] . "</td>";
    $tabla .= "<td>" . $los_clientes['TELEFONO'][$i] . "</td>";
    $tabla .= "<td>" . $los_clientes['ID_EMPRESA'][$i] . "</td>";
    $tabla .= "<td align='center'><a href='ver_cliente.php?i=" . $los_clientes['ID'][$i] . "' rel='boxed'><img src='images/menu/ver.png' border='0' title='Ver'></a>";
    $tabla .= "&nbsp; | &nbsp;<a href='#' onclick='javascript:borrar(" . $los_clientes['ID'][$i] . ");'><img src='images/menu/delete.png' border='0' title='Eliminar'></a></td></tr>";
  }
  $tabla .= "</tbody></table>";
}
?>

<html>
  <?php include('include.header_admin.html'); ?>
  <body class="bodyadmin">
    <div id="list_proveedores" align="center">

      <form id="frm_bp" name="frm_bp" method="post">
        <input type="hidden" id="ACCION" name="ACCION">
        <input type="hidden" id="id_cliente" name="id_cliente">
        <table class="marco">
          <tr>
            <td height="10%">
              <?php include('menu.php'); ?>
            </td>
          </tr>
          <tr height="50px">
            <td align="center" valign="button">
              Listado de clientes registrados
            </td>
          </tr>
          <tr>
            <td align="center" valign="top">
              <div id="DATO_HIDE" style="display:none" style="width:90%">
                <div id="DATO"></div>
              </div>
              <?php echo $tabla; ?>
            </td>
          </tr>
          <tr>
            <td height="10%" valign="bottom" align="center">
              <?php include('footer.php'); ?>
            </td>
          </tr>
        </table>
      </form>

    </div>
  </body>
  <?php include('include.footer_admin.html'); ?>
  <script>
    /****************** POP UP *****************/
    window.addEvent('domready', function() {
 
      SqueezeBox.assign($$('a[rel=boxed]'), {
                
        size: {x: 300, y: 300},
        ajaxOptions: {
          method: 'get' 
        }
      });
 
    });   
    ///////////////////////////////
    var myCheck;
    myCheck = new FormCheck('frm_bp', {
      submit:false,
      display:{
        errorsLocation : 1,
        indicateErrors : 2,
        keepFocusOnError : 0,
        closeTipsButton : 1,
        checkValueIfEmpty:1,                
        showErrors : 1
      }
    });		 
 
    $('frm_bp').addEvent('submit',function(ev){
      var IsValid;
      IsValid=myCheck.isFormValid();                                                                          
      if (IsValid){ 
            
        Ejecutor();
      }    
    });
    
    ///////////////////////////////
    
    
    function borrar(id){
	
      if(confirm("El cliente será eliminado,\n ¿Está de acuerdo?")){
        $('ACCION').value = "DELETE";
        $('id_cliente').value = id;
        Ejecutor();
      }
        
    }
    function modificar(id){
      window.location = "proveedor.php?i="+id;
    }
    function Ejecutor(){        
      window.addEvent('domready', function() {      	       
        var req = new Request.JSON({
          method: 'POST',
          url: 'ajax/ajax_registro_clientes.php', 
          data: $('frm_bp'),                
          noCache:true,                     
          onRequest: function(){
            $('DATO_HIDE').show();
            $('DATO').innerHTML = 'Procesando ...'; 
          },
          onComplete: function(){
            setTimeout(function(){
              $('DATO_HIDE').fade('out').get('tween').chain(function() {
                $('DATO_HIDE').hide();
                $('DATO_HIDE').fade('in');                                                         
              });
            },2000);                                                                                                                                                                                                              
          },             
          onSuccess: function(response){
            switch(parseInt(response.RESULT)){
              case 0:
                $('DATO').innerHTML = "Operaci&oacute;n exitosa";  
                window.location='list_clientes.php';
                break;
              case 1:
                $('DATO').innerHTML = response.ERROR;
                break;
            }
          }
        }).get();        
      });
    }
  </script>
</html>
